package com.hb.umsp.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hb.umsp.entity.*;
import com.hb.umsp.mapper.*;
import com.hb.umsp.util.MaxNoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class QuestionBankService {

    @Autowired
    HbQuestionBankMapper hbQuestionBankMapper;

    @Autowired
    HbQuestionAnsMapper hbQuestionAnsMapper;

    @Autowired
    MaxNoUtils maxNoUtils;

    @Transactional
    public void saveQuestionAndAns(List<HbQuestionBank> list) {

        if (list.size() < 1) {
            return;
        }
        HbQuestionBank hbQuestionBank = new HbQuestionBank();
        List<HbQuestionAns> hbQuestionAnsList = new ArrayList<>();
        for (int i=0; i<list.size(); i++) {
            hbQuestionBank = list.get(i);
            String questionCode = maxNoUtils.getIncrement("QuestionCode","Q",10);
            hbQuestionBank.setQuestionCode(questionCode);

            if (hbQuestionBank.getAnsOne() != null &&
                    !"".equals(hbQuestionBank.getAnsOne())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsOne(),questionCode));
            }
            if (hbQuestionBank.getAnsTwo() != null &&
                    !"".equals(hbQuestionBank.getAnsTwo())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsTwo(),questionCode));
            }
            if (hbQuestionBank.getAnsThree() != null &&
                    !"".equals(hbQuestionBank.getAnsThree())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsThree(),questionCode));
            }
            if (hbQuestionBank.getAnsFour() != null &&
                    !"".equals(hbQuestionBank.getAnsFour())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsFour(),questionCode));
            }
            if (hbQuestionBank.getAnsFive() != null &&
                    !"".equals(hbQuestionBank.getAnsFive())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsFive(),questionCode));
            }
            if (hbQuestionBank.getAnsSix() != null &&
                    !"".equals(hbQuestionBank.getAnsSix())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsSix(),questionCode));
            }
            if (hbQuestionBank.getAnsSeven() != null &&
                    !"".equals(hbQuestionBank.getAnsSeven())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsSeven(),questionCode));
            }
            if (hbQuestionBank.getAnsEight() != null &&
                    !"".equals(hbQuestionBank.getAnsEight())) {
                hbQuestionAnsList.add(getHbQuestionAns(
                        hbQuestionBank.getAnsEight(),questionCode));
            }
        }

        hbQuestionBankMapper.insert(list);
        hbQuestionAnsMapper.insert(hbQuestionAnsList);

    }

    public HbQuestionAns getHbQuestionAns(String ans,String questionCode){
        HbQuestionAns hbQuestionAns = new HbQuestionAns();
        hbQuestionAns.setQuestionCode(questionCode);

        String ansCode = maxNoUtils.getIncrement("ansCode","ANS",10);

        String[] arr = ans.split("#");
        hbQuestionAns.setAnsCode(ansCode);
        hbQuestionAns.setQuestionans(arr[0]);
        if (arr.length > 1) {
            hbQuestionAns.setQuestionAnsState(arr[1]);
        }
        return hbQuestionAns;
    }



    public PageInfo<HbQuestionBank> selectQuestionAndAns(int pageNum, int pageSize,
                                                         String token, HbQuestionBank hbQuestionBank) {
        PageHelper.startPage(pageNum, pageSize);

        List<HbQuestionBank> list = hbQuestionBankMapper.selectQuestionAndAns(hbQuestionBank);

        PageInfo page = new PageInfo(list);

        return page;
    }



    public static void main(String args[]) {
        QuestionBankService questionBankService = new QuestionBankService();
//        String a = questionBankService.getIncrement("A",10);
//        System.out.println(a);
//        HbQuestionBank hbQuestionBank = new HbQuestionBank();
//        List<HbQuestionBank> list = questionBankService.selectRandomList(hbQuestionBank,10);
//        System.out.println(list);

        int n = 5;
        Random rand = new Random();
        boolean[] bool = new boolean[n];
        int randInt = 0;
        for(int i = 0; i < 5 ; i++) {
            do {
                randInt = rand.nextInt(n);
            }while(bool[randInt]);
            bool[randInt] = true;
            System.out.println(randInt);
        }

    }
}
